/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.figure;

import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.core.figures.RoundedRectangleNodeFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

// TODO: Auto-generated Javadoc
public class ActionFigure extends RoundedRectangleNodeFigure {
	
	/**
	 * The Class ActionLabelFigure.
	 * Holds the names and the stereotypes
	 */
	protected class ActionLabelFigure extends NodeFigure {
		
		/**
		 * The Constructor.
		 */
		public ActionLabelFigure() {
			super();
			ToolbarLayout layout = new ToolbarLayout();
			setLayoutManager(layout);
			layout.setSpacing(2);
		}
	
		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics) {}
		
		/**
		 * Sets the bounds.
		 * 
		 * @param rect the rect
		 */
		@Override
		public void setBounds(Rectangle rect) {
			int labelHeight = 20;
			int labelWidth = getNameLabel().getPreferredSize().width + 4;
	
			// Calculate height
			if (getStereotypesLabel() != null) {
				labelHeight = labelHeight+ getStereotypesLabel().getPreferredSize().height;
			}
			if (getQualifiedNameLabel() != null) {
				labelHeight = labelHeight+ getQualifiedNameLabel().getPreferredSize().height;
			}
			if (getIconLabel() != null) {
				if (getIconLabel().getIcon() != null){
					labelHeight = labelHeight+ getIconLabel().getPreferredSize().height;
				}				
			}
			// Dimension of the rounded rectangle
			int fatherHeight = getParent().getSize().height;
			int fatherWidth = getParent().getSize().width;
			
			if (labelWidth > (fatherWidth - actionNameOffset)){
				labelWidth = fatherWidth - actionNameOffset;			
			}
			
			Rectangle labelRect = new Rectangle();
			labelRect.setLocation((fatherWidth - labelWidth - actionNameOffset) / 2 + actionNameLocationDelta, (fatherHeight - labelHeight) / 2);
			labelRect.setSize(labelWidth, labelHeight);
			
			super.setBounds(labelRect);
		}
	}

	/**
	 * display the content of the activity
	 * 
	 */
	protected class ActionContentFigure extends RoundedRectangleNodeFigure  {
	
		/**
		 * 
		 */
		private IFigure pane;
		
		/**
		 * 
		 */
		protected ScrollPane scrollpane;
	
		/**
		 * 
		 */
		public ActionContentFigure() {
			// Set content layout
			setLayoutManager(new StackLayout());
	
			// Add pane for content
			pane = new FreeformLayer();
			pane.setLayoutManager(new FreeformLayout());
			add(pane);
		}
	
		/**
		 * 
		 * 
		 * @return 
		 */
		public IFigure getContentPane() {
			return pane;
		}
	
		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics){	}
		
		/**
		 * Paint figure.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintFigure(Graphics graphics){	}
	}

	/** The action name offset. */
	protected int actionNameOffset;
	
	/** The action name location delta. */
	protected int actionNameLocationDelta;

	protected ActionLabelFigure labelFigure;
	/**
	 */
	protected ActionContentFigure contentFigure;
	
	public ActionFigure () {
		super();
		labelFigure = new ActionLabelFigure();
		add(labelFigure);
		contentFigure = new ActionContentFigure();
		add(contentFigure);
	}

	/**
	 * Gets the name label.
	 * 
	 * @return the name label
	 */
	@Override
	public Label getNameLabel() {
		return labelFigure.getNameLabel();
	}

	/**
	 * Gets the qualified name label.
	 * 
	 * @return the qualified name label
	 */
	@Override
	public Label getQualifiedNameLabel() {
		return labelFigure.getQualifiedNameLabel();
	}

	/**
	 * Gets the stereotypes label.
	 * 
	 * @return the stereotypes label
	 */
	@Override
	public Label getStereotypesLabel() {
		return labelFigure.getStereotypesLabel();
	}

	/**
	 * Refresh colors.
	 * 
	 * @param graphElement the graph element
	 */
	@Override
	public void refreshColors(GraphElement graphElement) {
		super.refreshColors(graphElement);
		labelFigure.setBackgroundColor(getBackgroundColor());
		labelFigure.setFontColor(getFontColor());
		labelFigure.setBorderColor(getBorderColor());
		labelFigure.setForeGroundColor(getForeGroundColor());
		
		Font baseFont = super.getNameLabel().getFont();
		if (baseFont.getFontData() != null){
			FontData baseFontData = baseFont.getFontData()[0];
			FontData[] labelFontData={new FontData(baseFontData.getName(), baseFontData.getHeight(), SWT.BOLD)};
			Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);			
			labelFigure.setFont(labelFont);
		}
	}

	/**
	 * Refresh presentation.
	 * 
	 * @param presentation the presentation
	 * @param selection the selection
	 */
	@Override
	public void refreshPresentation(String presentation, Element selection, GraphElement ge) {
		labelFigure.refreshPresentation(presentation, selection, ge);
	}

	/**
	 * Sets the bounds of the figure and its children.
	 * 
	 * @param rect the rect
	 */
	@Override
	public void setBounds(Rectangle rect) {
		labelFigure.setBounds(new Rectangle(rect));
		super.setBounds(rect);
	}

	/**
	 * Sets the name.
	 * 
	 * @param name the name
	 */
	@Override
	public void setName(String name) {
		labelFigure.setName(name);
	}

	/**
	 * Sets the qualified name.
	 * 
	 * @param qualifiedName the qualified name
	 */
	@Override
	public void setQualifiedName(String qualifiedName) {
		labelFigure.setQualifiedName(qualifiedName);
	}

	/**
	 * Sets the stereotypes.
	 * 
	 * @param stereotypes the stereotypes
	 */
	@Override
	public void setStereotypes(String stereotypes) {
		labelFigure.setStereotypes(stereotypes);
	}

	/**
	 * Use local coordinates.
	 * 
	 * @return true, if use local coordinates
	 */
	@Override
	protected boolean useLocalCoordinates(){return true;}

	/**
	 * 
	 * 
	 * @return 
	 */
	public IFigure getContentPane() {
		return contentFigure.getContentPane();	
	}
}

